package com.abigdreamer.wordpress.ui;

import java.io.File;
import java.util.List;

import com.abigdreamer.wordpress.newspider.FileUtil;
import com.zving.contentcore.bl.ContentBL;
import com.zving.contentcore.bl.SiteBL;
import com.zving.contentcore.code.ContentStatus;
import com.zving.framework.Config;
import com.zving.framework.UIFacade;
import com.zving.framework.User;
import com.zving.framework.annotation.Alias;
import com.zving.framework.annotation.Priv;
import com.zving.framework.collection.Mapx;
import com.zving.framework.data.DataRow;
import com.zving.framework.data.DataTable;
import com.zving.framework.data.Q;
import com.zving.framework.data.QueryBuilder;
import com.zving.framework.i18n.Lang;
import com.zving.framework.ui.IPageEnableAction;
import com.zving.framework.ui.control.DataListAction;
import com.zving.framework.utility.HtmlUtil;
import com.zving.framework.utility.ObjectUtil;
import com.zving.framework.utility.StringUtil;
import com.zving.framework.xml.XMLElement;
import com.zving.framework.xml.XMLMultiLoader;

/**
 * 
 * @author Darkness
 * @date 2013-6-28 下午11:53:51
 * @version V1.0
 */
@Alias("WordpressArticleList")
public class WordpressArticleListUI extends UIFacade {

	private DataTable getListData(IPageEnableAction action) {
		Q qb = new Q("select * from wordpress_article");

		action.setTotal(qb);
		DataTable dt = qb.fetch(action.getPageSize(), action.getPageIndex());

		return dt;
	}

	@Priv
	public void bindList(DataListAction dla) {

		// ArticleAnaly analy = new ArticleAnaly();
		// analy.process();

		DataTable dt = getListData(dla);

		if ((dt != null) && (dt.getRowCount() > 0)) {
			for (DataRow dr : dt) {
				String titleStyle = dr.getString("TitleStyle");
				titleStyle = StringUtil.replaceEx(titleStyle, ",", "");
				if (StringUtil.isNotNull(titleStyle)) {
					dr.set("TitleStyle", titleStyle);
				}
			}
		}
		dla.bindData(dt);
	}
	
	@Priv
	public void extractArticle() {
		String articleIDs = Request.getString("ArticleIDs");
		System.out.println("==articleIDs:" + articleIDs);
		String[] articleIDsArr = articleIDs.split(",");
		
		List<File> files = FileUtil.getAllFile("E:\\gather\\dixcus\\files", "xml");
		System.out.println(files.size());
		int i = 0;
		for (File xmlfile : files) {
			try {
				
			
			System.out.println("========>" + i++);
			XMLMultiLoader loader = new XMLMultiLoader();
			loader.load(xmlfile);
			
			XMLElement data = loader.getElements("article.id").get(0);
	        String articleId = data.getText();
//		}
//		
//		for (String string : articleIDsArr) {
//			String sql = "select * from wordpress_article where article_id=?";
//			QueryBuilder queryBuilder = new QueryBuilder(sql, string);
//			DataTable dataTable = queryBuilder.executeDataTable();
//			String articleId = dataTable.getString(0, "article_id");
			System.out.println("articleId:" + articleId);
			
			File file = new File("E:\\gather\\dixcus\\files\\"+ articleId + ".html");
			String content = WordpressArticleExtractor.extract(file);
			String updateSql = "update wordpress_article set content=? where article_id=?";
			new QueryBuilder(updateSql, content, articleId).execute();
			
			} catch (Exception e) {
				
			}
		}
		
	}
	
	@Priv
	public void quickEdit() {
		String articleId = Request.getString("ID");
		System.out.println("quickEdit===========:" + articleId);
		String sql = "select * from wordpress_article where id=?";
		QueryBuilder queryBuilder = new QueryBuilder(sql, articleId);
		DataTable dataTable = queryBuilder.executeDataTable();
		String content = dataTable.getString(0, "content");
		Response.put("Content", content);
	}
	
}
